﻿//   StorageItem.cs
//   Copyright (c) 2012 Eswar Prakash
// 
//    This library is free software; you can redistribute it and/or modify  
//    it  under the terms of the GNU Lesser General Public License version  
//    2.1 as published by the Free Software Foundation.                     
//                                                                          
//    This library is distributed in the hope that it will be useful, but   
//    WITHOUT ANY WARRANTY; without even the implied warranty of            
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     
//    Lesser General Public License for more details.                       
//                                                                          
//    You should have received a copy of the GNU Lesser General Public      
//    License along with this library; if not, write to the Free Software   
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
//    USA

namespace CKSAS.AppStorage.SharedService
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization;
    using Core.Utilities;
    using Microsoft.SharePoint;

    /// <summary>
    /// Represents the storage item information retrieved from the
    /// Application Storage service
    /// </summary>
    [DataContract(Name = "StorageItem", Namespace = "http://schemas.cksas.org/contracts/")]
    internal class StorageItem
    {
        /// <summary>
        /// Gets or sets the unique ID of the storage item
        /// </summary>
        [DataMember]
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the full name of the .NET type of the stored object
        /// </summary>
        [DataMember]
        public string TypeFullName { get; set; }

        /// <summary>
        /// Gets or sets the login name of the Owner of this item
        /// </summary>
        [DataMember]
        public string Owner { get; set; }

        /// <summary>
        /// Gets or sets the serialized object data related to the
        /// storage item
        /// </summary>
        [DataMember]
        public byte[] Data { get; set; }

        /// <summary>
        /// Gets or sets the created date of the item
        /// </summary>
        [DataMember]
        public DateTime CreatedDate { get; set; }

        /// <summary>
        /// Gets or sets the login name of the user who created this item
        /// </summary>
        [DataMember]
        public string CreatedBy { get; set; }

        /// <summary>
        /// Gets or sets the created date of the item
        /// </summary>
        [DataMember]
        public DateTime ModifiedDate { get; set; }

        /// <summary>
        /// Gets or sets the login name of the user who last modified this item
        /// </summary>
        [DataMember]
        public string ModifiedBy { get; set; }

        /// <summary>
        /// Creates a <see cref="StorageItem"/> from the provided object
        /// </summary>
        /// <param name="obj">The object which needs to be used to create the storage item.</param>
        /// <returns>An instance of <see cref="StorageItem"/> containing a serialized version of the object provided</returns>
        internal static StorageItem CreateFromObject(object obj)
        {
            // serialize to bytes
            var serialized = BinarySerializer.Serialize(obj);
            return new StorageItem()
                             {
                                 Data = serialized,
                                 TypeFullName = obj.GetType().AssemblyQualifiedName
                             };

        }
    }
}